package com.wondersgroup.framework.sample.component.demoform.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

import com.wondersgroup.framework.core.web.struts2.action.BaseAjaxAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.core5.model.vo.ValueObject;
import com.wondersgroup.framework.sample.component.demoform.vo.CheckTreeVO;
import com.wondersgroup.framework.sample.component.demoform.vo.CodeInfoVO;
import com.wondersgroup.framework.sample.component.demoform.vo.FullTreeVO;
import com.wondersgroup.framework.sample.component.demoform.vo.RadioTreeVO;
import com.wondersgroup.framework.sample.component.demoform.vo.TreeSelectSubmitVO;
import com.wondersgroup.framework.sample.component.demoform.vo.TreeSelectVO;
import com.wondersgroup.framework.sample.component.demoform.vs.DemoVS;
import com.wondersgroup.wssip.util.RequestUtils;

public class TreeSelectAction extends BaseAjaxAction {

	private HashMap<RadioTreeVO, String> radioTreeData = new LinkedHashMap<RadioTreeVO, String>();
	private HashMap<CheckTreeVO, String> checkTreeData = new LinkedHashMap<CheckTreeVO, String>();
	private List<FullTreeVO> fullTreeData = new ArrayList<FullTreeVO>();
	private boolean loadFlag;
	private DemoVS demoVS;
	private TreeSelectSubmitVO treeSelectSubmitVO = new TreeSelectSubmitVO();

	public TreeSelectAction(){
		this.initRadioTree();
		this.initCheckTree();
		this.initFullTree();
	}
	
	public TreeSelectSubmitVO getTreeSelectSubmitVO() {
		return treeSelectSubmitVO;
	}

	public void setTreeSelectSubmitVO(TreeSelectSubmitVO treeSelectSubmitVO) {
		this.treeSelectSubmitVO = treeSelectSubmitVO;
	}

	@Override
	public ValueObject getValueObject() {
		return this.treeSelectSubmitVO;
	}

	void initRadioTree() {
		radioTreeData.put(new RadioTreeVO("100","北京市","",false,false), "0");
		radioTreeData.put(new RadioTreeVO("101","东城区","",false,true), "100");
		radioTreeData.put(new RadioTreeVO("102","西城区","",false,true), "100");
		
		radioTreeData.put(new RadioTreeVO("200","上海市","",false,false), "0");
		radioTreeData.put(new RadioTreeVO("201","徐汇区","",false,true), "200");
		radioTreeData.put(new RadioTreeVO("202","黄浦区","",false,true), "200");
		
		radioTreeData.put(new RadioTreeVO("440000","广东省","",false,false), "0");
		radioTreeData.put(new RadioTreeVO("440100","广州市","",false,false), "440000");
		radioTreeData.put(new RadioTreeVO("440103","荔湾区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440104","越秀区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440105","海珠区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440106","天河区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440111","白云区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440112","黄埔区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440113","番禺区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440114","花都区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440115","南沙区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440116","萝岗区","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440183","增城市","",false,true), "440100");
		radioTreeData.put(new RadioTreeVO("440184","从化市","",false,true), "440100");
		
		radioTreeData.put(new RadioTreeVO("440300","深圳市","",false,false), "440000");
		radioTreeData.put(new RadioTreeVO("440303","罗湖区","",false,false), "440300");
		radioTreeData.put(new RadioTreeVO("440303001","桂园街道","",false,false), "440303");
		radioTreeData.put(new RadioTreeVO("440303001001","桂木园社区","",false,true), "440303001");
		radioTreeData.put(new RadioTreeVO("440303001002","人民桥社区","",false,true), "440303001");
		radioTreeData.put(new RadioTreeVO("440303001003","鹿丹村社区","",false,true), "440303001");
		radioTreeData.put(new RadioTreeVO("440303002","东晓街道","",false,true), "440303");
		radioTreeData.put(new RadioTreeVO("440303003","南湖街道","",false,true), "440303");
		
		radioTreeData.put(new RadioTreeVO("440304","福田区","",false,false), "440300");
		radioTreeData.put(new RadioTreeVO("440304001","福田街道","",false,true), "440304");
		radioTreeData.put(new RadioTreeVO("440304002","梅林街道","",false,true), "440304");
		radioTreeData.put(new RadioTreeVO("440304003","华强北街道","",false,true), "440304");
		radioTreeData.put(new RadioTreeVO("440305","南山区","",false,true), "440300");
		radioTreeData.put(new RadioTreeVO("440306","宝安区","",false,true), "440300");
		radioTreeData.put(new RadioTreeVO("440307","龙岗区","",false,true), "440300");
		radioTreeData.put(new RadioTreeVO("440308","盐田区","",false,true), "440300");
		radioTreeData.put(new RadioTreeVO("440309","光明新区","",false,true), "440300");
		radioTreeData.put(new RadioTreeVO("440310","坪山新区","",false,true), "440300");
		radioTreeData.put(new RadioTreeVO("440311","大鹏新区","",false,true), "440300");
		radioTreeData.put(new RadioTreeVO("440312","龙华新区","",false,true), "440300");
	}

	void initCheckTree() {
		checkTreeData.put(new CheckTreeVO("100","北京市","",false,false,false), "0");
		checkTreeData.put(new CheckTreeVO("101","东城区","",false,true,false), "100");
		checkTreeData.put(new CheckTreeVO("102","西城区","",false,true,false), "100");
		
		checkTreeData.put(new CheckTreeVO("200","上海市","",false,false,false), "0");
		checkTreeData.put(new CheckTreeVO("201","徐汇区","",false,true,false), "200");
		checkTreeData.put(new CheckTreeVO("202","黄浦区","",false,true,false), "200");
		
		checkTreeData.put(new CheckTreeVO("440000","广东省","",false,false,false), "0");
		checkTreeData.put(new CheckTreeVO("440100","广州市","",false,false,false), "440000");
		checkTreeData.put(new CheckTreeVO("440103","荔湾区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440104","越秀区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440105","海珠区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440106","天河区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440111","白云区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440112","黄埔区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440113","番禺区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440114","花都区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440115","南沙区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440116","萝岗区","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440183","增城市","",false,true,false), "440100");
		checkTreeData.put(new CheckTreeVO("440184","从化市","",false,true,false), "440100");
		
		checkTreeData.put(new CheckTreeVO("440300","深圳市","",false,false,false), "440000");
		checkTreeData.put(new CheckTreeVO("440303","罗湖区","",false,false,false), "440300");
		checkTreeData.put(new CheckTreeVO("440303001","桂园街道","",false,false,false), "440303");
		checkTreeData.put(new CheckTreeVO("440303001001","桂木园社区","",false,true,false), "440303001");
		checkTreeData.put(new CheckTreeVO("440303001002","人民桥社区","",false,true,false), "440303001");
		checkTreeData.put(new CheckTreeVO("440303001003","鹿丹村社区","",false,true,false), "440303001");
		checkTreeData.put(new CheckTreeVO("440303002","东晓街道","",false,true,false), "440303");
		checkTreeData.put(new CheckTreeVO("440303003","南湖街道","",false,true,false), "440303");
		
		checkTreeData.put(new CheckTreeVO("440304","福田区","",false,false,false), "440300");
		checkTreeData.put(new CheckTreeVO("440304001","福田街道","",false,true,false), "440304");
		checkTreeData.put(new CheckTreeVO("440304002","梅林街道","",false,true,false), "440304");
		checkTreeData.put(new CheckTreeVO("440304003","华强北街道","",false,true,false), "440304");
		checkTreeData.put(new CheckTreeVO("440305","南山区","",false,true,false), "440300");
		checkTreeData.put(new CheckTreeVO("440306","宝安区","",false,true,false), "440300");
		checkTreeData.put(new CheckTreeVO("440307","龙岗区","",false,true,false), "440300");
		checkTreeData.put(new CheckTreeVO("440308","盐田区","",false,true,false), "440300");
		checkTreeData.put(new CheckTreeVO("440309","光明新区","",false,true,false), "440300");
		checkTreeData.put(new CheckTreeVO("440310","坪山新区","",false,true,false), "440300");
		checkTreeData.put(new CheckTreeVO("440311","大鹏新区","",false,true,false), "440300");
		checkTreeData.put(new CheckTreeVO("440312","龙华新区","",false,true,false), "440300");
	}

	void initFullTree() {
		FullTreeVO node1 = new FullTreeVO("100","北京市","",false,false,false);
		FullTreeVO node11 = new FullTreeVO("101","东城区","",false,true,false);
		FullTreeVO node12 = new FullTreeVO("102","西城区","",false,true,false);
		
		FullTreeVO node2 = new FullTreeVO("200","上海市","",false,false,false);
		FullTreeVO node21 = new FullTreeVO("201","徐汇区","",false,true,false);
		FullTreeVO node22 = new FullTreeVO("202","黄浦区","",false,true,false);
		
		FullTreeVO node3 = new FullTreeVO("440000","广东省","",false,false,false);
		FullTreeVO node31 = new FullTreeVO("440100","广州市","",false,false,false);
		FullTreeVO node311 = new FullTreeVO("440103","荔湾区","",false,true,false);
		FullTreeVO node312 = new FullTreeVO("440104","越秀区","",false,true,false);
		FullTreeVO node313 = new FullTreeVO("440105","海珠区","",false,true,false);
		FullTreeVO node314 = new FullTreeVO("440106","天河区","",false,true,false);
		FullTreeVO node315 = new FullTreeVO("440111","白云区","",false,true,false);
		FullTreeVO node316 = new FullTreeVO("440112","黄埔区","",false,true,false);
		FullTreeVO node317 = new FullTreeVO("440113","番禺区","",false,true,false);
		FullTreeVO node318 = new FullTreeVO("440114","花都区","",false,true,false);
		FullTreeVO node319 = new FullTreeVO("440115","南沙区","",false,true,false);
		FullTreeVO node319_1 = new FullTreeVO("440116","萝岗区","",false,true,false);
		FullTreeVO node319_2 = new FullTreeVO("440183","增城市","",false,true,false);
		FullTreeVO node319_3 = new FullTreeVO("440184","从化市","",false,true,false);
		
		
		FullTreeVO node32 = new FullTreeVO("440300","深圳市","",false,false,false);
		FullTreeVO node321 = new FullTreeVO("440303","罗湖区","",false,false,false);
		FullTreeVO node3211 = new FullTreeVO("440303001","桂园街道","",false,false,false);
		FullTreeVO node32111 = new FullTreeVO("440303001001","桂木园社区","",false,true,false);
		FullTreeVO node32112 = new FullTreeVO("440303001002","人民桥社区","",false,true,false);
		FullTreeVO node32113 = new FullTreeVO("440303001003","鹿丹村社区","",false,true,false);
		
		FullTreeVO node3212 = new FullTreeVO("440303002","东晓街道","",false,true,false);
		FullTreeVO node3213 = new FullTreeVO("440303003","南湖街道","",false,true,false);
		
		FullTreeVO node322 = new FullTreeVO("440304","福田区","",false,false,false);
		FullTreeVO node3221 = new FullTreeVO("440304001","福田街道","",false,true,false);
		FullTreeVO node3222 = new FullTreeVO("440304002","梅林街道","",false,true,false);
		FullTreeVO node3223 = new FullTreeVO("440304003","华强北街道","",false,true,false);
		
		FullTreeVO node323 = new FullTreeVO("440305","南山区","",false,true,false);
		FullTreeVO node324 = new FullTreeVO("440306","宝安区","",false,true,false);
		FullTreeVO node325 = new FullTreeVO("440307","龙岗区","",false,true,false);
		FullTreeVO node326 = new FullTreeVO("440308","盐田区","",false,true,false);
		FullTreeVO node327 = new FullTreeVO("440309","光明新区","",false,true,false);
		FullTreeVO node328 = new FullTreeVO("440310","坪山新区","",false,true,false);
		FullTreeVO node329 = new FullTreeVO("440311","大鹏新区","",false,true,false);
		FullTreeVO node329_1 = new FullTreeVO("440312","龙华新区","",false,true,false);
		
		node1.getChildren().add(node11);
		node1.getChildren().add(node12);
		
		node2.getChildren().add(node21);
		node2.getChildren().add(node22);
		
		node3.getChildren().add(node31);
		node3.getChildren().add(node32);
		
		node31.getChildren().add(node311);
		node31.getChildren().add(node312);
		node31.getChildren().add(node313);
		node31.getChildren().add(node314);
		node31.getChildren().add(node315);
		node31.getChildren().add(node316);
		node31.getChildren().add(node317);
		node31.getChildren().add(node318);
		node31.getChildren().add(node319);
		node31.getChildren().add(node319_1);
		node31.getChildren().add(node319_2);
		node31.getChildren().add(node319_3);
		
		node32.getChildren().add(node321);
		node32.getChildren().add(node322);
		node32.getChildren().add(node323);
		node32.getChildren().add(node324);
		node32.getChildren().add(node325);
		node32.getChildren().add(node326);
		node32.getChildren().add(node327);
		node32.getChildren().add(node328);
		node32.getChildren().add(node329);
		node32.getChildren().add(node329_1);
		
		node321.getChildren().add(node3211);
		node321.getChildren().add(node3212);
		node321.getChildren().add(node3213);
		node322.getChildren().add(node3221);
		node322.getChildren().add(node3222);
		node322.getChildren().add(node3223);
		
		node3211.getChildren().add(node32111);
		node3211.getChildren().add(node32112);
		node3211.getChildren().add(node32113);
		
		this.fullTreeData.add(node1);
		this.fullTreeData.add(node2);
		this.fullTreeData.add(node3);
	}

	public DemoVS getDemoVS() {
		return demoVS;
	}

	public void setDemoVS(DemoVS demoVS) {
		this.demoVS = demoVS;
	}

	@Override
	protected String operate() {
		String result = prefix + suffex;
		return result;
	}

	public String loadRadioTreeSelect() {
		List<RadioTreeVO> result = new ArrayList<RadioTreeVO>();
		String parentId = RequestUtils.getParameter(this.servletRequest, "parentNodeId", "");
		String[] parentIds = (String[])this.radioTreeData.values().toArray(new String[this.radioTreeData.size()]);
		RadioTreeVO[] radioVOs = (RadioTreeVO[])this.radioTreeData.keySet().toArray(new RadioTreeVO[this.radioTreeData.size()]);
		for(int i=0;i<parentIds.length;i++){
			if(parentIds[i].equals(parentId)){
				result.add(radioVOs[i]);
			}
		}
		String json = VOUtils.getJsonDataFromCollection(result);
		createJSonData(json);
		return AJAX;
	}

	public String loadCheckTreeSelect() {
		List<CheckTreeVO> result = new ArrayList<CheckTreeVO>();
		String parentId = RequestUtils.getParameter(this.servletRequest, "parentNodeId", "");
		String[] parentIds = (String[])this.checkTreeData.values().toArray(new String[this.checkTreeData.size()]);
		CheckTreeVO[] radioVOs = (CheckTreeVO[])this.checkTreeData.keySet().toArray(new CheckTreeVO[this.checkTreeData.size()]);
		for(int i=0;i<parentIds.length;i++){
			if(parentIds[i].equals(parentId)){
				result.add(radioVOs[i]);
			}
		}
		String json = VOUtils.getJsonDataFromCollection(result);
		createJSonData(json);
		return AJAX;
	}

	public String loadFullTreeSelect() {
		String json = VOUtils.getJsonDataFromCollection(this.fullTreeData);
		createJSonData(json);
		return AJAX;
	}

	public String loadFormWithTreeSelect(){
		TreeSelectVO vo = new TreeSelectVO();
		CodeInfoVO city3 = new CodeInfoVO();
		city3.setValue("440100");
		city3.setText("广州市");
		vo.setCity3(city3);
		
		CodeInfoVO city4 = new CodeInfoVO();
		city4.setValue("440304001,440304002,440304003");
		city4.setText("福田街道,梅林街道,华强北街道");
		vo.setCity4(city4);
		String json = prefix+VOUtils.getJsonData(vo)+suffex;
		createJSonData(json);
		return AJAX;
	}
	
	public String submitFormWithTreeSelect(){
		System.out.println("city1="+treeSelectSubmitVO.getCity1());
		System.out.println("city2="+treeSelectSubmitVO.getCity2());
		System.out.println("city3="+treeSelectSubmitVO.getCity3());
		System.out.println("city4="+treeSelectSubmitVO.getCity4());
		createJSonData(prefix+suffex);
		return AJAX;
	}
}
